/******************************************************************************
 * 文件名：global_resources.cpp
 * 功能：全局资源管理实现文件
 *
 * 主要功能：
 * 1. 信号量和消息队列的创建与管理
 * 2. 线程安全的日志输出系统
 *
 * 作者：飞鸟
 * 版本：V1.0
 * 日期：2025-5-24
 *
 * 硬件依赖：
 * - CH32V307微控制器
 * - FreeRTOS实时操作系统
 ******************************************************************************/

#include "global_resources.h"


SemaphoreHandle_t CommunicationDevice_xBinarySemaphore = NULL;

QueueHandle_t ATM_Result_msg_Queue = NULL;
QueueHandle_t Tube_Pressure_Result_msg_Queue = NULL;
QueueHandle_t Tempareture_Result_msg_Queue = NULL;
QueueHandle_t WaterLevelCalculate_msg_Queue = NULL;
QueueHandle_t Timestamp_msg_Queue = NULL;
QueueHandle_t OTA_Update_Cmd_Queue = NULL;


TimerHandle_t xPeriodicTimer = NULL;

void Global_resources::Each_xBinarySemaphore_Init() {
    CommunicationDevice_xBinarySemaphore = xSemaphoreCreateBinary();
    if (CommunicationDevice_xBinarySemaphore == NULL) {
        CH32_LOGI("CommunicationDevice_xBinarySemaphore_Init", "failed to creat CommunicationDevice_xBinarySemaphore");
    }
}

void Global_resources::Msg_Queue_Init() {
    ATM_Result_msg_Queue = xQueueCreate(1, sizeof(float));
    if (ATM_Result_msg_Queue == NULL) {
        CH32_LOGI("Msg_Queue_Init", "failed to creat ATM_Result_msg_Queue");
    }
    Tube_Pressure_Result_msg_Queue = xQueueCreate(1, sizeof(float));
    if (Tube_Pressure_Result_msg_Queue == NULL) {
        CH32_LOGI("Msg_Queue_Init", "failed to creat Tube_Pressure_Result_msg_Queue");
    }
    Tempareture_Result_msg_Queue = xQueueCreate(1, sizeof(float));
    if (Tempareture_Result_msg_Queue == NULL) {
        CH32_LOGI("Msg_Queue_Init", "failed to creat Tempareture_Result_msg_Queue");
    }
    WaterLevelCalculate_msg_Queue = xQueueCreate(1, sizeof(WaterLevelCalculationParameters_Typedef));
    if (WaterLevelCalculate_msg_Queue == NULL) {
        CH32_LOGI("Msg_Queue_Init", "failed to creat WaterLevelCalculate_msg_Queue");
    }
    Timestamp_msg_Queue = xQueueCreate(1, timestamp_buffer_size);
    if (Timestamp_msg_Queue == NULL) {
        CH32_LOGI("Msg_Queue_Init", "failed to creat Timestamp_msg_Queue");
    }
    OTA_Update_Cmd_Queue = xQueueCreate(1, sizeof(OTA_Update_Cmd_Typedef));
    if (OTA_Update_Cmd_Queue == NULL) {
        CH32_LOGI("Msg_Queue_Init", "failed to creat OTA_Update_Cmd_Queue");
    }
}

void vPeriodicTimerCallback(TimerHandle_t xTimer) {

}

void Global_resources::Soft_Timer_Init() {

}



Global_resources::Global_resources() {
    Each_xBinarySemaphore_Init();
    Msg_Queue_Init();
    Soft_Timer_Init();
}


Global_resources::~Global_resources() {

}



/**
 * @brief 多线程下安全的日志打印
 * @param format字符串， 可变参数。如printf()的参数
 * @return none
 */
void Global_resources::LogOutput(const char* format, ...) {
//    /*
//     * 这是一个匿名函数（Lambda），用于封装互斥量的创建逻辑。
//     *   [] 表示不捕获任何外部变量
//     *   () 表示无参数
//     *   { ... } 是函数体
//     * 最后的 () 表示定义完 Lambda 后立即执行它
//     * 执行结果（return m）会赋值给 mutex
//     */​​
    static SemaphoreHandle_t mutex = [](){
                SemaphoreHandle_t m = xSemaphoreCreateMutex();
                if(!m) printf("\r\nMutex init failed!\r\n");
                return m;
            }();

    va_list args;
    va_start(args, format);

    if(xSemaphoreTake(mutex, pdMS_TO_TICKS(100)) == pdTRUE) {
        vprintf(format, args);
        xSemaphoreGive(mutex);
    }

    va_end(args);
}

